<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
            <!-- <el-form-item label="球员姓名" prop="no">
          <el-input v-model="queryParams.name" placeholder="请输入教练名称" clearable style="width: 150px;"
            @keyup.enter.native="handleQuery" />
        </el-form-item> -->
            <el-form-item label="球队名" prop="no">
                <el-select v-model="queryParams.teamId" placeholder="请选择球队">
                    <el-option v-for="item in mtBasketballTeamList" :key="item.id" :label="item.name" :value="item.id">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <!-- <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button> -->
                <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                    v-hasPermi="['merchant:index']">添加战绩</el-button>
            </el-form-item>
        </el-form>

        <el-table ref="tables" v-loading="loading" :data="list" @selection-change="handleSelectionChange"
            :default-sort="defaultSort" @sort-change="handleSortChange">
            <el-table-column label="ID" prop="id" width="55" />
            <el-table-column label="甲队名称" align="center" prop="aname" />
            <el-table-column label="甲队得分" align="center" prop="ascore" />
            <!-- <el-table-column label="介绍" align="center" prop="introduce" /> -->
            <!-- <el-table-column label="机构简介" align="center" prop="intro" /> -->
            <!-- <el-table-column label="创建时间" align="center" prop="createTime"/> -->
            <el-table-column label="乙队名称" align="center" prop="bname" />
            <el-table-column label="乙队得分" align="center" prop="bscore" />
            <!-- <el-table-column label="比赛日期" align="center" prop="matchDate" /> -->
            <el-table-column label="比赛日期" align="center" prop="matchDate" sortable="custom"
                :sort-orders="['descending', 'ascending']" width="180">
                <template slot-scope="scope">
                    <span>{{ formatDate(scope.row.matchDate) }}</span>
                </template>
            </el-table-column>
            <el-table-column label="开始时间" align="center">
                <template slot-scope="scope">
                    {{ formatTime(scope.row.startTime) }}
                </template>
            </el-table-column>
            <el-table-column label="结束时间" align="center">
                <template slot-scope="scope">
                    {{ formatTime(scope.row.endTime) }}
                </template>
            </el-table-column>
            <!-- <el-table-column label="开始时间" align="center" prop="startTime" sortable="custom"
                :sort-orders="['descending', 'ascending']" width="180">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.startTime) }}</span>
                </template>
            </el-table-column>
            <el-table-column label="结束时间" align="center" prop="endTime" sortable="custom"
                :sort-orders="['descending', 'ascending']" width="180">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.endTime) }}</span>
                </template>
            </el-table-column> -->
            <el-table-column label="创建时间" align="center" prop="createTime" sortable="custom"
                :sort-orders="['descending', 'ascending']" width="180">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.createTime) }}</span>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-edit" v-hasPermi="['merchant:index']"
                        @click="handleUpdate(scope.row)">修改</el-button>
                    <el-button size="mini" type="text" icon="el-icon-delete" v-hasPermi="['merchant:index']"
                        @click="handleDelete(scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.page" :limit.sync="queryParams.pageSize"
            @pagination="getList" />

        <!-- 添加或修改对话框 -->
        <el-dialog :title="title" :visible.sync="open" class="common-dialog" width="500px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="130px">
                <el-row>
                    <el-col :span="24">
                        <el-form-item label="甲球队" prop="aId">
                            <el-select v-model="form.aid" placeholder="请选择球队">
                                <el-option v-for="item in mtBasketballTeamList" :key="item.id" :label="item.name"
                                    :value="item.id" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="24">
                        <el-form-item label="甲队得分" prop="ascore">
                            <el-input v-model="form.ascore" placeholder="请输入甲队得分" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="24">
                        <el-form-item label="乙球队" prop="bid">
                            <el-select v-model="form.bid" placeholder="请选择球队">
                                <el-option v-for="item in mtBasketballTeamList" :key="item.id" :label="item.name"
                                    :value="item.id" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="24">
                        <el-form-item label="乙队得分" prop="bscore">
                            <el-input v-model="form.bscore" placeholder="请输入乙队得分" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="24">
                        <el-form-item label="比赛日期" prop="style">
                            <!-- <el-date-picker v-model="form.matchDate" type="datetime" placeholder="选择日期时间"></el-date-picker> -->
                            <el-date-picker v-model="form.matchDate" type="date" placeholder="选择比赛日期"></el-date-picker>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="24">
                        <el-form-item label="开始时间" prop="style">
                            <!-- <el-date-picker v-model="form.startTime" type="datetime" placeholder="选择日期时间"></el-date-picker> -->
                            <el-time-select v-model="form.startTime" :picker-options="{
                                start: '00:00',
                                step: '01:00',
                                end: '24:00'
                            }" placeholder="选择时间">
                            </el-time-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="24">
                        <el-form-item label="结束时间" prop="style">
                            <!-- <el-date-picker v-model="form.endTime" type="datetime" placeholder="选择日期时间"></el-date-picker> -->
                            <el-time-select v-model="form.endTime" :picker-options="{
                                start: '00:00',
                                step: '01:00',
                                end: '24:00'
                            }" placeholder="选择时间">
                            </el-time-select>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确定</el-button>
                <el-button @click="cancel">取消</el-button>
            </div>
        </el-dialog>
    </div>
</template>
    
<script>
import { getToken } from '@/utils/auth';
import { getMtStatsList, saveSetting, update, deleteMtStats, getMtStatsInfor } from "@/api/mtstatus";
export default {
    name: "MtcoachList",
    // components: { txMap },
    data() {
        return {
            // 遮罩层
            loading: true,
            // 标题
            title: "",
            // 选中数组
            ids: [],
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 表格数据
            list: [],
            // 是否显示弹出层
            open: false,
            // 搜索地点结果
            locationList: [],
            // 选中地址id
            locationValue: null,
            // 选中地址详情
            locationItem: {},
            // 默认排序
            defaultSort: { prop: 'createTime', order: 'descending' },
            // 商户类型
            typeOptions: [],
            //机构名称
            mtBasketballTeamList: [],
            // 表单参数
            form: {},
            // 上传地址
            uploadAction: process.env.VUE_APP_SERVER_URL + 'backendApi/file/upload',
            // 隐藏上传
            hideUpload: false,
            // 上传文件列表
            uploadFiles: [],
            uploadHeader: { 'Access-Token': getToken() },
            // 图片根目录
            imagePath: "",
            // 查询参数
            queryParams: {
                page: 1,
                pageSize: 10,
                teamId: null
            },
            // 表单校验
            rules: {

            }
        };
    },
    created() {
        this.getList();
    },
    methods: {
        // 查询日志
        getList() {
            this.loading = true;
            getMtStatsList(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
                this.list = response.data.mtStatsList.content;
                this.total = response.data.mtStatsList.totalElements;
                this.imagePath = response.data.mtStatsList.imagePath;
                this.typeOptions = response.data.mtStatsList.typeList;
                this.mtBasketballTeamList = response.data.mtBasketballTeamList;        
                this.loading = false;
            });
        },
        formatDate(timestamp) {
            const date = new Date(timestamp);
            const year = date.getFullYear();
            const month = String(date.getMonth() + 1).padStart(2, '0');
            const day = String(date.getDate()).padStart(2, '0');
            return `${year}-${month}-${day}`;
        },
        // 搜索按钮操作
        handleQuery() {
            this.queryParams.page = 1;
            this.getList();
        },
        formatTime(timestamp) {
            const date = new Date(timestamp);
            return `${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`;
        },

        // 重置按钮操作
        resetQuery() {
            this.resetForm("queryForm");
            this.$refs.tables.sort(this.defaultSort.prop, this.defaultSort.order)
            this.handleQuery();
        },

        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id);
            this.multiple = !selection.length;
        },
        // 排序触发事件
        handleSortChange(column, prop, order) {
            this.queryParams.orderByColumn = column.prop;
            this.queryParams.isAsc = column.order;
            this.getList();
        },
        // 新增按钮操作
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "新增战绩";
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                aid: null,
                ascore: null,
                bid: null,
                bscore: null,
                matchDate: null,
                startTime: null,
                endTime: null
            };
            this.resetForm("form");
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 提交按钮
        submitForm: function () {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.id) {
                        update(this.form).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        saveSetting(this.form).then(response => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        // 修改按钮操作
        handleUpdate(row) {
            const app = this;
            app.reset();
            const id = row.id || this.ids;
            getMtStatsInfor(id).then(response => {
                app.form = response.data.mtStats;
                app.form.startTime = this.formatTimestamp(app.form.startTime)
                app.form.endTime = this.formatTimestamp(app.form.endTime)
                app.open = true;
                app.title = "编辑战绩信息";
            });
        },
        formatTimestamp(timestamp) {
            const date = new Date(timestamp);
            const hours = ('0' + date.getHours()).slice(-2);
            const minutes = ('0' + date.getMinutes()).slice(-2);
            return `${hours}:${minutes}`;
        },
        // 删除按钮操作
        handleDelete(row) {
            this.id;
            const aname = row.aname;
            const bname = row.bname;
            this.$modal.confirm('是否确认删除"' + aname + '"和"' + bname + '"的比赛战绩？').then(function () {
                return deleteMtStats(row.id);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => { });
        },
        handleUploadSuccess(file) {
            this.form.logo = file.data.fileName;
        }
    }
};
</script>
<style scoped>
.common-dialog>>>.el-upload--picture-card {
    width: 60px;
    height: 50px;
    line-height: 60px;
}

.el-input {
    width: 220px;
}
</style>
    
    